package com.scalar.chiptrack.operations.dataentry.transit.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.dataentry.transit.forms.TransitDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.actioncommands.ActionCommand;

import java.util.HashMap;
import java.util.ArrayList;
import java.sql.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Oct 12, 2006
 * Time: 8:21:34 PM
 */
public class TransitDataEntryShopOrderDetailsCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/transitDataEntry.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        TransitDataEntryForm transitDataEntryForm = (TransitDataEntryForm) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(com.scalar.chiptrack.commons.SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(com.scalar.chiptrack.commons.SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList processTypeList;
        ArrayList lotList;
        ArrayList partNumberList;
        ArrayList locationList;
        ArrayList nextLocationList;      
        ArrayList poDetailsList;
        ArrayList shopOrderList;
        ArrayList fabLotNumberList;
        PartNumber partNumber;
        VendorLocation location;
        Vendor vendor;

        ArrayList transitPartNumbersLableValueBeans     = new ArrayList();
        ArrayList transitLocationsLableValueBeans       = new ArrayList();
        ArrayList transitLotNumberLabelValueBeans       = new ArrayList();
        ArrayList transitPoNumbersLabelValueBeans       = new ArrayList();
        ArrayList transitShopOrderLabelValueBeans       = new ArrayList();
        ArrayList transitfabLotNumberLabelValueBeans    = new ArrayList();
        ArrayList transitNextLocationsLabelValueBeans   = new ArrayList();

        try
        {
            LotDetails lotDetails = new LotDetails();
            processTypeList = lotDetails.getTransitProcessList();
            partNumberList = DataEntryDAO.getPartNumber(transitDataEntryForm.getProcessType());

            for (int i = 0; i < partNumberList.size(); i++)
            {
                partNumber = (PartNumber) partNumberList.get(i);
                transitPartNumbersLableValueBeans.add(new org.apache.struts.util.LabelValueBean(partNumber.getPartNumber(), partNumber.getProcessStepId()));
            }

            locationList = DataEntryDAO.getSelectedLocations(Integer.parseInt(transitDataEntryForm.getProcessStepId()));
            for (int i = 0; i < locationList.size(); i++)
            {
                location = (VendorLocation) locationList.get(i);
                transitLocationsLableValueBeans.add(new org.apache.struts.util.LabelValueBean(location.getLocationName(), location.getLocationID()));
            }

            fabLotNumberList = DataEntryDAO.getTransitFabLots( Integer.parseInt(transitDataEntryForm.getProcessStepId()), Integer.parseInt(transitDataEntryForm.getLocationId()) );
            for (int i = 0; i < fabLotNumberList.size(); i++)
            {
                lotDetails = (LotDetails) fabLotNumberList.get(i);
                transitfabLotNumberLabelValueBeans.add(new org.apache.struts.util.LabelValueBean( lotDetails.getFabLotNumber(), lotDetails.getFabLotNumber()));
            }

            lotList = DataEntryDAO.getTransitLots(transitDataEntryForm.getProcessStepId(), transitDataEntryForm.getFabLotNo());
            for (int i = 0; i < lotList.size(); i++)
            {
                lotDetails = (LotDetails) lotList.get(i);
                transitLotNumberLabelValueBeans.add(new org.apache.struts.util.LabelValueBean(lotDetails.getLotNumber(), lotDetails.getShippedLotId()));
            }

            LotDetails transitLotDetails = transitDataEntryForm.getTransitLotDetails();
            if (transitLotDetails.getShipDate() == null)
            {
                transitLotDetails.setShipDate(StringUtils.formatDateToString(new Date(System.currentTimeMillis()), "MM/dd/yyyy"));
            }

            poDetailsList = DataEntryDAO.getPOandShopOrderNo( transitDataEntryForm.getProcessStepId(), transitDataEntryForm.getLocationId() );
            for (int i = 0; i < poDetailsList.size(); i++)
            {
                lotDetails = (LotDetails) poDetailsList.get(i);
                transitPoNumbersLabelValueBeans.add(new org.apache.struts.util.LabelValueBean(lotDetails.getPoNumber(), lotDetails.getPoId()));
            }

            shopOrderList = DataEntryDAO.getShopOrderNo( transitDataEntryForm.getPoId() );
            if( shopOrderList != null && shopOrderList.size() > 0 )
            {
                for (int i = 0; i < shopOrderList.size(); i++)
                {
                    lotDetails = (LotDetails) shopOrderList.get(i);
                    transitShopOrderLabelValueBeans.add(new org.apache.struts.util.LabelValueBean(lotDetails.getShopOrderNo(), lotDetails.getShopOrderNo()));
                }
            }

            nextLocationList = DataEntryDAO.getNextLocations();
            for( int i = 0; i < nextLocationList.size(); i++ )
            {
                vendor = (Vendor) nextLocationList.get(i);
                transitNextLocationsLabelValueBeans.add(new org.apache.struts.util.LabelValueBean( vendor.getVendorName(), vendor.getVendorName()));
            }

            //nextLocationList = transitLotDetails.getTransitNextLocations();
            transitDataEntryForm.setTransitLotDetails(transitLotDetails);

            request.setAttribute("processTypeList", processTypeList);
            request.setAttribute("partNumbersList", transitPartNumbersLableValueBeans);
            request.setAttribute("locationList", transitLocationsLableValueBeans);
            request.setAttribute("transitFabLotNoList", transitfabLotNumberLabelValueBeans);
            request.setAttribute("transitList", transitLotNumberLabelValueBeans);
            request.setAttribute("nextLocationList", transitNextLocationsLabelValueBeans);
            request.setAttribute("poNumbersList", transitPoNumbersLabelValueBeans);
            request.setAttribute("shopOrderNoList", transitShopOrderLabelValueBeans);

            request.setAttribute("action", action);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return mapping.findForward("transitDataEntry");
    }
}
